package com.tencent.wxcloudrun.service.business;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencent.wxcloudrun.config.UserContext;
import com.tencent.wxcloudrun.pojo.bo.NoticeReadEnum;
import com.tencent.wxcloudrun.pojo.bo.NoticeStateEnum;
import com.tencent.wxcloudrun.pojo.dto.resp.GetNoticeStateListResp;
import com.tencent.wxcloudrun.pojo.dto.resp.NoticeInfoResp;
import com.tencent.wxcloudrun.pojo.po.XHelpmateInfo;
import com.tencent.wxcloudrun.pojo.po.XNoticeManage;
import com.tencent.wxcloudrun.service.XHelpmateInfoService;
import com.tencent.wxcloudrun.service.XNoticeManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NoticeService {

    @Resource
    private XNoticeManageService xNoticeManageService;

    @Resource
    private XHelpmateInfoService xHelpmateInfoService;

    public Integer getNoticeCount(String pageType) {
        String openId = UserContext.getOpenId();
        if ("tenant".equals(pageType)) { // 租客的时候
            LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserOpenId, openId);
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserType, "tenant");
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getNoticeRead, "no");
            long count = xNoticeManageService.count(xNoticeManageLambdaQueryWrapper);
            return (int) count;
        }
        // 检查用户是不是助理
        LambdaQueryWrapper<XHelpmateInfo> xHelpmateInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xHelpmateInfoLambdaQueryWrapper.eq(XHelpmateInfo::getHelpmateId, openId);
        XHelpmateInfo one = xHelpmateInfoService.getOne(xHelpmateInfoLambdaQueryWrapper);
        if (ObjectUtil.isNotNull(one) && StrUtil.isNotBlank(one.getHelpmateId())) { // 助理的时候
            LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserOpenId, openId);
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserType, "helpmate");
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getNoticeRead, "no");
            long count = xNoticeManageService.count(xNoticeManageLambdaQueryWrapper);
            return (int) count;
        } else { // 房东的情况
            LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserOpenId, openId);
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserType, "land");
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getNoticeRead, "no");
            long count = xNoticeManageService.count(xNoticeManageLambdaQueryWrapper);
            return (int) count;
        }
    }

    public List<NoticeInfoResp> getNoticeList(String pageType) {
        List<NoticeInfoResp> respList = new ArrayList<>();
        String openId = UserContext.getOpenId();
        if ("tenant".equals(pageType)) { // 租客的时候
            LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserOpenId, openId);
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserType, "tenant");
            xNoticeManageLambdaQueryWrapper.orderByDesc(XNoticeManage::getNoticeState, XNoticeManage::getCreateTime);
            List<XNoticeManage> list = xNoticeManageService.list(xNoticeManageLambdaQueryWrapper);
            for (XNoticeManage manage : list) {
                NoticeInfoResp noticeInfoResp = new NoticeInfoResp();
                noticeInfoResp.setNoticeId(manage.getNoticeId());
                noticeInfoResp.setNoticeTitle(manage.getNoticeTitle());
                noticeInfoResp.setNoticeState(NoticeReadEnum.findByKey(manage.getNoticeRead()).getValue() + "-" +
                        NoticeStateEnum.findByKey(manage.getNoticeState()).getValue());
                noticeInfoResp.setNoticeTime(DateUtil.format(manage.getCreateTime(), DatePattern.NORM_DATETIME_FORMAT));
                respList.add(noticeInfoResp);
            }
            return respList;
        }
        // 检查用户是不是助理
        LambdaQueryWrapper<XHelpmateInfo> xHelpmateInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xHelpmateInfoLambdaQueryWrapper.eq(XHelpmateInfo::getHelpmateId, openId);
        XHelpmateInfo one = xHelpmateInfoService.getOne(xHelpmateInfoLambdaQueryWrapper);
        if (ObjectUtil.isNotNull(one) && StrUtil.isNotBlank(one.getHelpmateId())) { // 助理的时候
            LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserOpenId, openId);
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserType, "helpmate");
            xNoticeManageLambdaQueryWrapper.orderByDesc(XNoticeManage::getNoticeState, XNoticeManage::getCreateTime);
            List<XNoticeManage> list = xNoticeManageService.list(xNoticeManageLambdaQueryWrapper);
            for (XNoticeManage manage : list) {
                NoticeInfoResp noticeInfoResp = new NoticeInfoResp();
                noticeInfoResp.setNoticeId(manage.getNoticeId());
                noticeInfoResp.setNoticeTitle(manage.getNoticeTitle());
                noticeInfoResp.setNoticeState(NoticeReadEnum.findByKey(manage.getNoticeRead()).getValue() + "-" +
                        NoticeStateEnum.findByKey(manage.getNoticeState()).getValue());
                noticeInfoResp.setNoticeTime(DateUtil.format(manage.getCreateTime(), DatePattern.NORM_DATETIME_FORMAT));
                respList.add(noticeInfoResp);
            }
            return respList;
        } else { // 房东的情况
            LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserOpenId, openId);
            xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getUserType, "land");
            xNoticeManageLambdaQueryWrapper.orderByDesc(XNoticeManage::getNoticeState, XNoticeManage::getCreateTime);
            List<XNoticeManage> list = xNoticeManageService.list(xNoticeManageLambdaQueryWrapper);
            for (XNoticeManage manage : list) {
                NoticeInfoResp noticeInfoResp = new NoticeInfoResp();
                noticeInfoResp.setNoticeId(manage.getNoticeId());
                noticeInfoResp.setNoticeTitle(manage.getNoticeTitle());
                noticeInfoResp.setNoticeState(NoticeReadEnum.findByKey(manage.getNoticeRead()).getValue() + "-" +
                        NoticeStateEnum.findByKey(manage.getNoticeState()).getValue());
                noticeInfoResp.setNoticeTime(DateUtil.format(manage.getCreateTime(), DatePattern.NORM_DATETIME_FORMAT));
                respList.add(noticeInfoResp);
            }
            return respList;
        }
    }

    public List<GetNoticeStateListResp> getNoticeStateList() {
        List<GetNoticeStateListResp> respList = new ArrayList<>();
        GetNoticeStateListResp resp1 = new GetNoticeStateListResp();
        resp1.setKey("2");
        resp1.setValue("待处理");
        GetNoticeStateListResp resp2 = new GetNoticeStateListResp();
        resp2.setKey("1");
        resp2.setValue("无标识");
        GetNoticeStateListResp resp3 = new GetNoticeStateListResp();
        resp3.setKey("0");
        resp3.setValue("已处理");
        respList.add(resp1);
        respList.add(resp2);
        respList.add(resp3);
        return respList;
    }

    public String updateNoticeState(String noticeId, String noticeState) {
        XNoticeManage xNoticeManage = new XNoticeManage();
        xNoticeManage.setNoticeId(noticeId);
        xNoticeManage.setNoticeState(noticeState);
        xNoticeManage.setUpdateTime(new Date());
        xNoticeManageService.updateById(xNoticeManage);
        return "success";
    }

    @Transactional
    public String getNoticeContent(String noticeId) {
        LambdaQueryWrapper<XNoticeManage> xNoticeManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xNoticeManageLambdaQueryWrapper.eq(XNoticeManage::getNoticeId, noticeId);
        XNoticeManage xNoticeManage = xNoticeManageService.getOne(xNoticeManageLambdaQueryWrapper);
        XNoticeManage xNoticeManage1 = new XNoticeManage();
        xNoticeManage1.setNoticeId(noticeId);
        xNoticeManage1.setNoticeRead("yes");
        if (ObjectUtil.isNull(xNoticeManage.getReadTime())) {
            xNoticeManage1.setReadTime(new Date());
            xNoticeManageService.updateById(xNoticeManage1);
        }
        xNoticeManageService.updateById(xNoticeManage1);
        return xNoticeManage.getNoticeContent();
    }

    @Transactional
    public String createNotice(String userType, String title, String content) {
        String idStr = IdUtil.getSnowflakeNextIdStr();
        String openId = UserContext.getOpenId();
        Date date = new Date();
        XNoticeManage xNoticeManage = new XNoticeManage();
        xNoticeManage.setNoticeId(idStr);
        xNoticeManage.setUserType(userType);
        xNoticeManage.setUserOpenId(openId);
        xNoticeManage.setNoticeTitle(title);
        xNoticeManage.setNoticeContent(content);
        xNoticeManage.setNoticeRead(NoticeReadEnum.no.getKey());
        xNoticeManage.setNoticeState(NoticeStateEnum.ONE.getKey());
        xNoticeManage.setCreateTime(date);
        xNoticeManage.setUpdateTime(date);
        xNoticeManageService.save(xNoticeManage);
        return idStr;
    }

    @Transactional
    public String createNoticeByOpenid(String openId, String userType, String title, String content) {
        String idStr = IdUtil.getSnowflakeNextIdStr();
        Date date = new Date();
        XNoticeManage xNoticeManage = new XNoticeManage();
        xNoticeManage.setNoticeId(idStr);
        xNoticeManage.setUserType(userType);
        xNoticeManage.setUserOpenId(openId);
        xNoticeManage.setNoticeTitle(title);
        xNoticeManage.setNoticeContent(content);
        xNoticeManage.setNoticeRead(NoticeReadEnum.no.getKey());
        xNoticeManage.setNoticeState(NoticeStateEnum.ONE.getKey());
        xNoticeManage.setCreateTime(date);
        xNoticeManage.setUpdateTime(date);
        xNoticeManageService.save(xNoticeManage);
        return idStr;
    }
}
